<?php
namespace Admin\Controller;
use Admin\Model\MemberModel;

class MemberController extends AdminBaseController
{
    public function add()
    {
    	if(IS_POST)
    	{
			$data['status'] = I('post.status', 0);
    		$model = new MemberModel();
    		if($model->create(I('post.'), 1))
    		{
    			if($id = $model->add())
    			{
    				$this->success('添加成功！', U('lst?p='.I('get.p')));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}
    	
    	// 取出所有的角色
    	$roleModel = M('Role');
    	$roleData = $roleModel->select();
    	$this->assign('roleData', $roleData);

		$this->display();
    }
    public function edit()
    {
    	// 要修改的管理员的ID
    	$id = I('get.id');
    	// 先判断是否有权修改
    	$adminId = session('id');   // 取出当前管理员的ID
    	// 如果是普通管理员要修改其他管理员的信息提示无权
    	if($adminId > 1 && $adminId != $id)
    		$this->error('无权修改！');
    	
    	if(IS_POST)
    	{
    		$model = D('Admin/Admin');
    		if($model->create(I('post.'), 2))
    		{
    			if($model->save() !== FALSE)
    			{
    				$this->success('修改成功！', U('lst', array('p' => I('get.p', 1))));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}
    	$model = M('Admin');
    	$data = $model->find($id);
    	$this->assign('data', $data);
    	
    	// 取出所有的角色
    	$roleModel = M('Role');
    	$roleData = $roleModel->select();
    	$this->assign('roleData', $roleData);
    	// 取出当前管理员所在的角色的ID
    	$arModel = M('AdminRole');
    	$rid = $arModel->field('GROUP_CONCAT(role_id) role_id')->where(array('admin_id'=>array('eq', $id)))->find();
    	$this->assign('rid', $rid['role_id']);
    	

		$this->setPageBtn('修改管理员', '管理员列表', U('lst?p='.I('get.p')));
		$this->display();
    }
    public function delete()
    {
    	$model = new MemberModel();
    	if($model->delete(I('get.id', 0)) !== FALSE)
    	{
    		$this->success('删除成功！', U('lst', array('p' => I('get.p', 1))));
    		exit;
    	}
    	else 
    	{
    		$this->error($model->getError());
    	}
    }
    public function lst()
    {
    	$model = new MemberModel();
    	$data = $model->search();
    	$this->assign(array(
    		'data' => $data['data'],
    		'page' => $data['page'],
    	));

    	$this->display();
    }

	//ajax修改密码
	public function editpwd(){

		$oldpwd = I('post.oldpwd');
		$newpwd1 = I('post.newpwd1');
		$newpwd2 = I('post.newpwd2');

		$model = new MemberModel();
		if(session('a_pwd') == md5($oldpwd.C('MD5'))){
			$info = $model->updatepwd($newpwd1);
			if(info == 1){
				echo "修改成功！";
			}else{
				echo "修改失败！";
			}
		}else{
			echo "密码错误！";
		}


		//echo $oldpwd.'--'.$newpwd1.'--'.$newpwd2;
		//echo "1111222";
		//$this->ajaxReturn("123","修改成功!",1);

	}

}